package com.yuanqunsoft.alimigration.service.impl;

import java.io.File;
import java.util.List;

import com.yuanqunsoft.alimigration.dao.IModuleDao;
import com.yuanqunsoft.alimigration.model.Module;
import com.yuanqunsoft.alimigration.service.IModuleService;

public class ModuleServiceImpl implements IModuleService {
    private IModuleDao moduleDao;

    public IModuleDao getModuleDao() {
        return moduleDao;
    }

    public void setModuleDao(IModuleDao moduleDao) {
        this.moduleDao = moduleDao;
    }

    @Override
    public boolean addModule(Module instance) {
        try {
            this.getModuleDao().addModule(instance);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean renameReport(String name, Integer modId) {
        try {
            this.getModuleDao().renameModule(name, modId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteModule(Module instance) {
        try {
            System.out.println("###############:"+instance.getModId());
//            this.getModuleDao().deleteModule(instance);
            deleteFileById(instance.getModId());
            
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean updateModule(Module instance) {
        try {
            this.getModuleDao().updateModule(instance);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteByApplicatoinId(Integer appId) {
        try{
            this.getModuleDao().deleteByApplicationId(appId);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public List<Module> findByApplicationId(Integer appId) {
        return this.getModuleDao().findByApplicationId(appId);
    }

    @Override
    public Module findById(Integer id) {
        return this.getModuleDao().findById(id);
    }

    @Override
    public boolean deleteFileById(Integer id) {
        Module instance = this.findById(id);
        System.out.println("###########FileUrl to delete:"+instance.getModFileUrl());
        File file = new File(instance.getModFileUrl());
        file.delete();
        
        return false;
    }

}
